<template>
  <section class="todoapp">
    <!-- 除了驼峰, 还可以使用-转换链接 -->
    <TodoHeader :list="list"></TodoHeader>
    <TodoMain :list="showList"></TodoMain>
    <TodoFooter
      :list="list"
      @changeType="changeFn"
      :flag="flag"
      @clearFn="clearFn"
    ></TodoFooter>
  </section>
</template>

<script>
// 1.0 样式引入
import "./styles/base.css";
import "./styles/index.css";

import TodoHeader from "./components/TodoHeader";
import TodoMain from "./components/TodoMain";
import TodoFooter from "./components/TodoFooter";

export default {
  data() {
    return {
      list: JSON.parse(localStorage.getItem("todolist")) || [],
      flag: "all",
    };
  },
  methods: {
    changeFn(val) {
      this.flag = val;
    },
    clearFn() {
      this.list = this.list.filter((item) => item.isDone === false);
    },
  },
  components: {
    TodoHeader,
    TodoMain,
    TodoFooter,
  },
  computed: {
    showList() {
      if (this.flag === "all") return this.list;
      else if (this.flag === "yes")
        return this.list.filter((item) => item.isDone === true);
      else return this.list.filter((item) => item.isDone === false);
    },
  },
  watch: {
    list: {
      deep: true,
      handler() {
        localStorage.setItem("todolist", JSON.stringify(this.list));
      },
    },
  },
};
</script>
